<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.List"%>
<%@ page import="com.google.appengine.api.datastore.*"%>
<%@ page import="com.google.appengine.api.memcache.*"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Praktikaportaal | Ettevõte</title>
</head>
<body>
	<%@ include file="header.jsp"%>
	<div id="content">
	<%
		Long id = Long.parseLong(request.getParameter("id"));
		DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
		Key companyKey = KeyFactory.createKey("Company", id);
		Entity company = null;
		try {
			company = ds.get(companyKey);
			String name = (String) company.getProperty("name");
			String description = ((Text) company.getProperty("description")).getValue();
	%>	
		<div class="content-container">
			<div class="grid">
				<div class="row">
					<div class="c3">
						<img class="framed-image" src="/static/img/logo_2.png">
					</div>
					<div class="c6">
						<h1 class="tight">Nimi</h1>
						<p class="light">Tegevusvaldkond</p>
					</div>
					<div class="c3 end" id="company-rate-widget">
						<div class="rating-star" id="star-1"></div>
						<div class="rating-star" id="star-2"></div>
						<div class="rating-star" id="star-3"></div>
						<div class="rating-star" id="star-4"></div>
						<div class="rating-star" id="star-5"></div>
					</div>
				</div>
			</div>
		</div>
		<div class="content-container">
			<h1>Tööpakkumised</h1>
	<%
			Query query = new Query("Offer").setAncestor(companyKey);
    		List<Entity> offers = ds.prepare(query).asList(FetchOptions.Builder.withDefaults());
    		if (offers.isEmpty()) {
	%>
			<p>Ettevõttel ei ole tööpakkumisi</p>
	<%
    		} else {
	%>
			<div id="company-offers">
	<%
				for (Entity o : offers) {
					String position = (String) o.getProperty("position");
					String offerDescription = ((Text) o.getProperty("description")).getValue();
	%>
    			<h3><%= position %></h3>
				<div><%= offerDescription %></div>
	<%
				}
	%>
			</div>
	<%
    		}
	%>
		</div>
		
		<div class="content-container">
			<h1>Kommentaarid</h1>
	<%
			query = new Query("Comment", companyKey).addSort("date", Query.SortDirection.DESCENDING);
    		List<Entity> comments = ds.prepare(query).asList(FetchOptions.Builder.withLimit(10));
    		if (comments.isEmpty()) {
	%>
			<p>Selle ettevõtte kohta kommentaarid puuduvad.</p>
	<%
    		} else {
				for (Entity c : comments) {
					String body = ((Text) c.getProperty("body")).getValue();
	%>
				<div><%= body %></div>
				<hr>
	<%
				}
    		}
	%>
		</div>
		<div class="content-container">
			<h1>Jäta oma kommentaar</h1>
			<form action="/postcomment" method="post">
				<textarea id="body" name="body"></textarea>
				<br>
				<input type="submit" value="Salvesta"></input>
			</form>
		</div>
		
	<%
		} catch (EntityNotFoundException e) {
	%>
		<div class="content-container">
			<p>Ettevõtet ei leitud.</p>
		</div>
	<%
		}
	%>
	</div>
	<%@ include file="footer.jsp"%>
</body>
</html>